package com.ruoyi.classes.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.classes.domain.ClassStudentRelationship;
import com.ruoyi.classes.domain.ClassesVo;
import com.ruoyi.classes.mapper.ClassStudentRelationshipMapper;
import com.ruoyi.classes.service.IClassStudentRelationshipService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.student.domain.StuInfo;
import com.ruoyi.student.mapper.StuInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.classes.mapper.ClassesMapper;
import com.ruoyi.classes.domain.Classes;
import com.ruoyi.classes.service.IClassesService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 班级管理Service业务层处理
 * 
 * @author yiang
 * @date 2025-04-10
 */
@Service
public class ClassesServiceImpl implements IClassesService 
{
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private StuInfoMapper stuInfoMapper;
    @Resource
    private ClassStudentRelationshipMapper classStudentRelationshipMapper;

    /**
     * 查询班级管理
     * 
     * @param classId 班级管理主键
     * @return 班级管理
     */
    @Override
    public Classes selectClassesByClassId(Long classId)
    {
        return classesMapper.selectClassesByClassId(classId);
    }

    /**
     * 查询班级管理列表
     * 
     * @param classes 班级管理
     * @return 班级管理
     */
    @Override
    public List<Classes> selectClassesList(Classes classes)
    {
        return classesMapper.selectClassesList(classes);
    }

    /**
     * 新增班级管理
     * 
     * @param classes 班级管理
     * @return 结果
     */
    @Override
    public int insertClasses(Classes classes)
    {
        return classesMapper.insertClasses(classes);
    }

    /**
     * 修改班级管理
     * 
     * @param classes 班级管理
     * @return 结果
     */
    @Override
    public int updateClasses(Classes classes)
    {
        return classesMapper.updateClasses(classes);
    }

    /**
     * 批量删除班级管理
     * 
     * @param classIds 需要删除的班级管理主键
     * @return 结果
     */
    @Override
    public int deleteClassesByClassIds(Long[] classIds)
    {
        return classesMapper.deleteClassesByClassIds(classIds);
    }

    /**
     * 删除班级管理信息
     * 
     * @param classId 班级管理主键
     * @return 结果
     */
    @Override
    public int deleteClassesByClassId(Long classId)
    {
        return classesMapper.deleteClassesByClassId(classId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult transClass(ClassesVo classes) {
        StuInfo stuInfo = new StuInfo();
        stuInfo.setStudentId(String.valueOf(classes.getStudentId()));
        stuInfo.setClassName(classes.getClasses().getClassName());
        stuInfo.setClassId(String.valueOf(classes.getClasses().getClassId()));
        stuInfoMapper.updateStuInfo(stuInfo);
        ClassStudentRelationship classStudentRelationship1 = classStudentRelationshipMapper.selectClassStudentRelationshipByStuId(classes.getStudentId());
        if (classStudentRelationship1 == null) {
            return AjaxResult.warn("学生未加入任何班级");
        }
        Long originalClassId = classStudentRelationship1.getClassId();
        classStudentRelationshipMapper.deleteByStuIdAndClassId(classes.getStudentId(),originalClassId);
        ClassStudentRelationship classStudentRelationship = new ClassStudentRelationship();
        classStudentRelationship.setStudentId(classes.getStudentId());
        classStudentRelationship.setClassId(classes.getClasses().getClassId());
        classStudentRelationshipMapper.insertClassStudentRelationship(classStudentRelationship);
        return AjaxResult.success(" 转班成功 ");
    }

}
